
export const HOST = "";  

export const SetLS = (key, json) => {
    try {
        window.localStorage.setItem(key, JSON.stringify(json));
    } catch(_) {
        alert("本地储存写入错误，若为safari浏览器请关闭无痕模式浏览。");
    }
}
export const GetLS = key => {
    var str = window.localStorage.getItem(key);
    if (str != null && str != "")
        return JSON.parse(str);
    else
        return false;
}
export const ClearLS = key => {
    if (key)
        window.localStorage.removeItem(key);
    else
        window.localStorage.clear();
}
export const openWin = (url) => {
    if (url) {
        window.location.href = url;
    }
}
export const replaceWin = (url) => {
    if (url) {
        location.replace(url);
    }
}

export const setTitle = (title) => {
    let sT = function(t) {
        document.title = t;
        let i = document.createElement('iframe');
        i.src = 'http://www.lunzi.online/static/img/logo.cc58945.png';
        i.style.display = 'none';
        i.onload = function() {
            setTimeout(function(){
                i.remove();
            }, 9);
        }
        document.body.appendChild(i);
    }
    setTimeout(function(){
        sT(title);
    }, 100);
}

export const getUrlParam = (key, url) => {
    var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)", "i"), url = url ? (url.split("?")[1]||'') : window.location.href.split("?")[1], r;
    if (url){
        r = url.match(reg);
    }
    return r ? decodeURIComponent(r[2]) : "";
}

export const isPhone = (phone) => {

    return /^1[34578][0-9]{1}\d{8}$/.test(phone);

}

/* 
* url 目标url 
* arg 需要替换的参数名称 
* arg_val 替换后的参数的值 
* return url 参数替换后的url 
*/ 
export const changeURLArg = (url,arg,arg_val) => { 
    let pattern=arg+'=([^&]*)'; 
    let replaceText=arg+'='+arg_val; 
    if(url.match(pattern)){ 
        let tmp='/('+ arg+'=)([^&]*)/gi'; 
        tmp=url.replace(eval(tmp),replaceText); 
        return tmp; 
    }else{ 
        if(url.match('[\?]')){ 
            return url+'&'+replaceText; 
        }else{ 
            return url+'?'+replaceText; 
        } 
    } 
    return url+'\n'+arg+'\n'+arg_val; 
} 

export const dealURLParam = (obj, url) => {  //url 默认加上 参数
    for (let key in obj){
        sessionStorage[key] = obj[key];
        if (key != "token" && obj[key] != "") {
            if (obj[key] == "undefined"){
                obj[key] = "";
            }
            url = changeURLArg(url, key, obj[key])
        }
    }
    window.history.replaceState(document.title, "", url);
} 


export const changeHash = (key, value) => {  //无刷新改变hash值

    let url = changeURLArg(location.href, key, value)

    window.history.replaceState(document.title, "", url);
}


export const isMobile = () => {

    if((navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i))) {
        
        return true;

    } else {

        return false
    }
}

export const getPlatform = () => {

    let u = navigator.userAgent;
    let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
    let isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端

    if (isAndroid) {

        return 'android'
    }

    if (isiOS){

        return 'ios'
    }

    return 'others'
}
export const getCookie = name => {
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
    if (arr = document.cookie.match(reg))
        return unescape(arr[2]);
    else
        return null;
}

export const isWechat = (() => {

    return /micromessenger/i.test(navigator.userAgent);
})()

/* url */
export const isUrl = url => {

    return /^((ht|f)tps?):\/\/([\w-]+(\.[\w-]+)*\/?)+(\?([\w\-\.,@?^=%&:\/~\+#]*)+)?/.test(url);
}

export const getLocalTime = (ms,day) => {
    if(!ms){
        return "";
    }
    ms = Number(ms);
    var _date = new Date(ms);
    var year=_date.getFullYear(),
        month=_date.getMonth()+1,
        date=_date.getDate(),
        hour=_date.getHours(),
        minute=_date.getMinutes(),
        second=_date.getSeconds();
    return year+"-"+(month<10 ? ("0"+month) : month)+"-"+(date<10 ? ("0"+date) : date)+ 
        (!day ? (" "+(hour<10 ? ("0"+hour) : hour)+":"+(minute<10?("0"+minute):minute)+":"+(second<10?("0"+second):second)) : ""); 
}

export const getLocalTimeStr = (ms,day) => {
    if(!ms){
        return "";
    }
    if (!isNaN(Number(ms))){
        ms = Number(ms);
    }
    var _date = new Date(ms);
    var year=_date.getFullYear(),
        month=_date.getMonth()+1,
        date=_date.getDate(),
        hour=_date.getHours(),
        minute=_date.getMinutes(),
        second=_date.getSeconds();
    return year+"-"+(month<10 ? ("0"+month) : month)+"-"+(date<10 ? ("0"+date) : date)+ 
        (!day ? (" "+(hour<10 ? ("0"+hour) : hour)+":"+(minute<10?("0"+minute):minute)+":"+(second<10?("0"+second):second)) : ""); 
}

export const getTime = (time,ms) => {
    if(!time){
        return "";
    }
    time = Number(time);
    var _date = new Date(time);
    var hour=_date.getHours(),
        minute=_date.getMinutes(),
        second=_date.getSeconds();
        
    return (hour<10 ? ("0"+hour) : hour)+":"+(minute<10?("0"+minute):minute) + (ms ? (":"+(second<10?("0"+second):second)) : '' ); 
}

export const getCloseDate = (dateNum) => {
    var minute = 1000 * 60,
        hour = minute * 60,
        day = hour * 24,
        halfamonth = day * 15,
        month = day * 30;

    var now = new Date().getTime(),
        diffValue = now - dateNum,
        monthC = diffValue / month,
        weekC = diffValue / (7 * day),
        dayC = diffValue / day,
        hourC = diffValue / hour,
        minC = diffValue / minute,
        result = "";

    if (monthC >= 1) {
        result = parseInt(monthC) + "个月前";
    } else if (weekC >= 1) {
        result = parseInt(weekC) + "周前";
    } else if (dayC >= 1) {
        result = parseInt(dayC) + "天前";
    } else if (hourC >= 1) {
        result = parseInt(hourC) + "小时前";
    } else if (minC >= 1) {
        result = parseInt(minC) + "分钟前";
    } else
        result = "刚刚";

    return result;
}

export const getCloseDateFormat = (dateCha) => {

    if (parseFloat(dateCha) === NaN){
        return dateCha;
    }
    var minute = 1000 * 60,
        hour = minute * 60,
        day = hour * 24,
        halfamonth = day * 15,
        month = day * 30;

    var now = new Date().getTime(),
        // diffValue = Math.abs(now - dateNum),
        diffValue = Math.abs(dateCha),
        monthC = diffValue / month,
        weekC = diffValue / (7 * day),
        dayC = diffValue / day,
        hourC = diffValue / hour,
        minC = diffValue / minute,
        result = "";

    if (monthC >= 1) {
        result = parseInt(monthC) + "个月";
    } else if (weekC >= 1) {
        result = parseInt(weekC) + "周";
    } else if (dayC >= 1) {
        result = parseInt(dayC) + "天";
    } else if (hourC >= 1) {
        result = parseInt(hourC) + "小时";
    } else if (minC >= 1) {
        result = parseInt(minC) + "分钟";
    } else {
        result = "0小时";
    }

    let str = '';
    if (dateCha >= 0){
        str = '剩余' + result;
    } else {
        str = result + '后开放';
    }

    return str;
}

export const switchUnit = (str, noText) => {

    
    let num = parseFloat(str) || 0;

    let plusFlag = num >= 0 ? true : false;

    num = Math.abs(num)

    // if (num > 0 && num < 1){
    //     num = num.toFixed(4);
    // } else if (num >= 1 && num < 1000){
    //     num = num.toFixed(2);
    // } else if (num >= 1000 && num < 10000){
    //     num = num.toFixed(2);
    // } else if (num >= 10000 && num < 100000){
    //     num = (num/10000).toFixed(2) + '万';
    // } else if (num >= 100000 && num < 1000000){
    //     num = (num/10000).toFixed(1) + '万';
    // } else if (num >= 1000000 && num < 100000000){
    //     num = (num/10000).toFixed(0) + '万';
    // } else if (num >= 100000000 && num < 10000000000){
    //     num = (num/100000000).toFixed(1) + '亿';
    // } else if (num >= 10000000000){
    //     num = (num/100000000).toFixed(0) + '亿';
    // } 

    if (num > 0 && num < 1){
        num = num.toFixed(4);
    } else if (num >= 1 && num < 100){
        num = num.toFixed(3);
    } else if (num >= 100){
        num = num.toFixed(4);
    } 
    if (plusFlag == false){
        num = '-' + num;
    }
// console.log(plusFlag, num, str)

    return num === 0 ? noText : num;
}


export const switchPrice = (str, noText) => {

    var num = parseFloat(str) || 0;

    let plusFlag = num >= 0 ? true : false;

    num = Math.abs(num)

    if (num > 0 && num < 0.0001){
        num = num.toFixed(8);
    } else if (num >= 0.0001 && num < 0.001){
        num = num.toFixed(8);
    } else if (num >= 0.001 && num < 0.01){
        num = num.toFixed(6);
    } else if (num >= 0.01 && num < 0.1){
        num = num.toFixed(5);
    } else if (num >= 0.1 && num < 100){
        num = num.toFixed(4);
    } else if (num >= 100 && num < 100000000){
        num = num.toFixed(2);
    }
    if (plusFlag == false){
        num = '-' + num;
    }

    return num === 0 ? noText : num;
}

export const switchExgName = exg => {

    let str = '';
    switch(exg){
        case 'okef':
            str = 'OKEx交割';
            break;
        case 'okswap':
            str = 'OKEx永续';
            break;
        case 'bitmex':
            str = 'BITMEX';
            break;
        default:
            str = exg;
            break;
    }
    return str;
}


export const Guid = () => {

    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
        var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
        return v.toString(16);
    });
}

/* 格式化数字 三位加逗号 */
export function toThousands(num) {
    let nnum = parseFloat(num) || 0;
    if (nnum === 0){
        return num;
    }
    num = num.toString();
    let lStr = num.split(".")[0], rStr = num.split(".")[1] || false;

    return lStr.replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') + (rStr ? '.' + rStr : '');
}

/* 邮箱 */
export function validateEmail(str) {
    const reg = /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/
    return reg.test(str)
}

/* 隐藏手机中间4位 */
export function tel(phone) {
    return phone.replace(/(\d{3})\d{4}(\d{4})/, "$1****$2")
}

